package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.GameDao;
import com.example.peanutsubway.entity.Game;
import com.example.peanutsubway.entity.PageInfo;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.vo.GameType;

import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/10/10-20:05
 * @Since:jdk1.8
 * @Description:
 */


public class GameDaoImpl implements GameDao {

    @Override
    public List<GameType> showAllGame(Game game, PageInfo pageInfo) {
        String sql = "SELECT gd.*,gt.typename from pn_game_type gt,pn_game gd WHERE gt.typeid = gd.tid and gd.gstatus = 1 ";
        if (game != null) {
            if (game.getGname() != null && !"".equals(game.getGname())) {
                sql += " and gname like '%" + game.getGname() + "%' ";
            }
        }
        sql += (" limit ?,?");
        return JDBCUtils.find(sql, GameType.class, pageInfo.getStartIndex(), pageInfo.getPageNum());
    }

    @Override
    public Integer count(Game game) {
        String sql = "SELECT count(1) from pn_game_type gt,pn_game gd WHERE  gt.typeid = gd.tid  and gd.gstatus = 1 ";
        if (game != null) {
            if (game.getGname() != null && !"".equals(game.getGname())) {
                sql += " and gname like '%" + game.getGname() + "%' ";
            }
        }
        return JDBCUtils.count(sql).intValue();
    }

    @Override
    public List<Game> iosUrlList(Game game) {
        String sql = "select iosurl from pn_game";
        return JDBCUtils.find(sql, Game.class);
    }

    @Override
    public List<Game> androidUrlList(Game game) {
        String sql = "select androidurl from pn_game";
        return JDBCUtils.find(sql, Game.class);
    }

    @Override
    public List<Game> gameiconUrlList(Game game) {
        String sql = "select gameicon from pn_game";
        return JDBCUtils.find(sql, Game.class);
    }

    @Override
    public List<Game> gamepictureList(Game game) {
        String sql = "select prictures from pn_game";
        return JDBCUtils.find(sql, Game.class);
    }

    @Override
    public Boolean addGame(Game game) {
        String sql = "INSERT INTO pn_game VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?, NULL,NULL,?,NULL)";
        return JDBCUtils.insert(sql, game.getGname(), game.getTitle(), game.getDownloadCount(), game.getSize(), game.getDes(), game.getGameIcon(), game.getIosUrl(), game.getAndroidUrl(),
                game.getPrictures(), game.getGstatus(), game.getTid(), game.getRecType(), game.getPlatform()) > 0;
    }

    @Override
    public Boolean updateGame(Game game) {
        String sql = "UPDATE pn_game set gname=?,title=?,size=?,des=?,gameicon=?,iosurl=?,androidurl=?,prictures=?,gstatus=?,tid=?,rectype=?,platform=? where gid = ?";
        return JDBCUtils.update(sql, game.getGname(), game.getTitle(), game.getSize(), game.getDes(), game.getGameIcon(), game.getIosUrl(), game.getAndroidUrl(), game.getPrictures(),
                game.getGstatus(), game.getTid(), game.getRecType(), game.getPlatform(), game.getGid()) > 0;
    }

    @Override
    public Boolean deleteGame(Integer gid) {
        String sql = "update  pn_game set gstatus = 0 where gid = ?";
        return JDBCUtils.update(sql, gid) > 0;
    }
}
